package com.zoncont.estate.service.impl;

import com.google.common.collect.Lists;
import com.zoncont.common.core.domain.Ztree;
import com.zoncont.common.utils.StringUtils;
import com.zoncont.estate.domain.Menu;
import com.zoncont.estate.mapper.SaasMenuMapper;
import com.zoncont.estate.service.ISaasMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜单 业务层处理
 *
 * @author zoncont.ahanu
 */
@Service
public class SaasMenuServiceImpl implements ISaasMenuService {

    @Autowired
    private SaasMenuMapper saasMenuMapper;

    /**
     * 根据公司查询菜单
     *
     * @param companyId 公司ID
     * @return 菜单列表
     */
    @Override
    public List<String> selectMenuTree(String companyId){
        return saasMenuMapper.selectMenuTree(companyId);
    }

    /**
     * 查询公司菜单
     *
     * @param menuList     菜单列表
     * @param companyMenuList 公司已存在菜单列表
     * @return 菜单列表
     */
    @Override
    public List<Ztree> menuTreeDataByCompany(List<Menu> menuList, List<String> companyMenuList) {
        return initZtree(menuList, companyMenuList);
    }

    /**
     * 查询菜单使用数量
     *
     * @param menuName 菜单名称
     * @return 结果
     */
    @Override
    public int selectCountByMenuName(String menuName) {
        return saasMenuMapper.selectCountByMenuName(menuName);
    }

    /**
     * 对象转菜单树
     *
     * @param menuList     菜单列表
     * @param companyMenuList 公司已存在菜单列表
     * @return 树结构列表
     */
    private List<Ztree> initZtree(List<Menu> menuList, List<String> companyMenuList) {
        List<Ztree> ztrees = Lists.newArrayList();
        boolean isCheck = StringUtils.isNotNull(companyMenuList);
        for (Menu menu : menuList) {
            Ztree ztree = new Ztree();
            ztree.setId(menu.getMenuId());
            ztree.setpId(menu.getParentId());
            ztree.setName(menu.getMenuName());
            ztree.setTitle(menu.getMenuName());
            if (isCheck) {
                ztree.setChecked(companyMenuList.contains(menu.getMenuName() + menu.getPerms()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
